/** @odoo-module **/
// 单个行数据
const getRowItemData = (owl,data,otherCompany) => {
    let {dish_top_category,cooking_category,dish_key,dish_material_color_category,dish_material_shape_category,plan_top_category,number_category,dinner_type_category,cus_loc}=owl;
    let {past_week_1,past_week_2} = owl.index.model_history_week; //past_week_1 上周菜品 past_week_2 上上周菜品

    if(otherCompany){//其他公司
        number_category = otherCompany.number_category;
        dish_key = otherCompany.dish_key;
        dinner_type_category = otherCompany.dinner_type_category;
        past_week_1 = [];
        past_week_2 = [];
    }
    let v = JSON.parse(JSON.stringify(data));
    let is_te_pei = !!v.te_pei_json;
    if(is_te_pei){
        v.dish_top_category_id = 4;
    }
    let day_dish = {};//周1-周日菜品
    for(let i = 1; i　< 8; i++) {
        let _dish = dish_key[v['day'+i+'_key_id']] ||{};
        day_dish['day'+i + '_dish_key'] = _dish;
        day_dish['day'+i + '_dish_key_name'] = _dish.name || '';
        if(is_te_pei){
            try{
                v.te_pei_json[`day${i}`].forEach(pei_id=>{
                    let te_pei_name = owl.formatterTePeiIdToStr(pei_id);
                    day_dish['day'+i + '_dish_key_name'] += ('/'+te_pei_name)
                })
            }catch (e){}
        }
        day_dish['day'+i + '_dish_key_cost'] = _dish.dish_cost || '';
        let color_id = _dish.color_id;
        day_dish['day'+i + '_dish_color_code'] = dish_material_color_category[color_id] && dish_material_color_category[color_id].color_code;
        day_dish['day'+i + '_dish_key_easy_meal'] = _dish.easy_meal === 'Y' || _dish.easy_meal === true ?'下饭':'';
        let shape_id = _dish.shape_id;
        day_dish['day'+i + '_dish_key_shape'] = dish_material_shape_category[shape_id] && dish_material_shape_category[shape_id].name;//丁,串,段....
        day_dish['day'+i + '_dish_key_spicy'] = _dish.spicy;

        let plan_top_category_id = _dish.plan_top_category_id;
        day_dish['day'+i + '_dish_key_category'] = plan_top_category[plan_top_category_id] && plan_top_category[plan_top_category_id].id;
        day_dish['day'+i + '_dish_key_category_name'] = plan_top_category[plan_top_category_id] && plan_top_category[plan_top_category_id].name;

        if(!v['day'+i + '_limit_category_id']){ //类别id不存在或0  默认为菜品的类别
            v['day'+i + '_limit_category_id'] = day_dish['day'+i + '_dish_key_category'];
        }
        v['day'+i + '_limit_category_name'] = plan_top_category[v['day'+i + '_limit_category_id']] && plan_top_category[v['day'+i + '_limit_category_id']].name;

        // 是不是炸品
        day_dish['day'+i +'_isFried'] = cooking_category[_dish.cooking_method_id] && cooking_category[_dish.cooking_method_id].name.includes('炸');
        // 是不是绿叶菜
        day_dish['day'+i +'_isGreen'] = dish_material_color_category[color_id] && dish_material_color_category[color_id].name.includes('绿色') && v.dish_top_category_id != 5;//不考虑汤

        // 重复
        let filterFn=(d)=>{
            // v.dinner_type == 'dn2' ? '午餐' : v.dinner_type == 'dn3' ? '晚餐' : v.dinner_type == 'dn5' ? '夜餐' : '早餐'
            // && (v.dinner_type == 'dn2' || v.dinner_type == 'dn3')
            return d == v['day'+i+'_key_id']
        };
        day_dish['day'+i + '_past_week_1'] = (past_week_1 || []).filter(filterFn).length;
        day_dish['day'+i + '_past_week_2'] = (past_week_2 || []).filter(filterFn).length;
    }
    let dish_top_category_name_cn = dish_top_category[v.dish_top_category_id] && dish_top_category[v.dish_top_category_id].name_cn || '';//大荤 小荤 素菜...
    let dish_num_id_name = number_category[v.dish_num_id] && number_category[v.dish_num_id].name || '';//大荤1 大荤2 ......
    let isSpecialGroups = v.sales_type == 'special' && v.dish_top_category_id != 4;//是特色组的配菜  v.sales_type == 'kuai'/'special'
    if(isSpecialGroups){
        // dish_num_id_name = `${dish_top_category_name_cn}(${dish_num_id_name})`;
        dish_num_id_name = `- ${dish_top_category_name_cn}`;
    }
    if(is_te_pei){
        try {
            dish_num_id_name = cus_loc[v.cus_loc_id].plan_day_cus_name;
        }catch (e) {}
        // console.log(dish_num_id_name);
    }

    v.number_category = number_category[v.dish_num_id] ||{};
    v.dish_top_category_name_cn = dish_top_category_name_cn;
    v.dinner_type = dinner_type_category[v.dinner_type_id].dinner_type;
    v.dinner_type_name = dinner_type_category[v.dinner_type_id].dinner_type_name;
    v.dinner_type_category_name = dinner_type_category[v.dinner_type_id].name;
    v.dinner_type_category = dinner_type_category[v.dinner_type_id] ||{};
    v.dish_num_id_name = dish_num_id_name;
    v.isSpecialGroups = isSpecialGroups;

    Object.assign(v, day_dish);

    return v;
}

const getRowData = (owl,planWeekData,otherCompany) =>{
    let data = [];
    // console.log(dish_key[3094096].name);
    planWeekData.forEach((v) => {
        if(v.isDeleted){
            return;
        }
        data.push(getRowItemData(owl,v,otherCompany))
    });
    console.log('--------ag-grid的rowData----------');
    console.log(data);
    return data;
};

const clearLimitCategoryId = (planWeekData) =>{
    planWeekData.forEach((v) => {
        for(let i = 1; i　< 8; i++) {
            v['day'+i + '_limit_category_id'] = 0
        }
    });
}

const getCountRowData = (data) =>{
    let Count = {
        dish_key_id:{},
        num_and_top_category:{},
    };
    data.forEach(v=>{
        if(v.isDeleted){
            return;
        }
        for(let i = 1; i　< 8; i++) {
            !Count['day'+i] && (Count['day'+i] = {});
            //绿色菜 辣度 只考虑快餐  炸数量看全部菜品
            !Count['day'+i][v.dinner_type_id] && (Count['day'+i][v.dinner_type_id] = {fried:0});

            !Count['day'+i][v.dinner_type_id][v.sales_type] && (Count['day'+i][v.dinner_type_id][v.sales_type] = {
                green:0,
                spicy:0,
                total:0
            });

            if(!!v['day'+i+'_dish_key_spicy'] && v['day'+i+'_dish_key_spicy'] != 'spicy1'){//辣度
                Count['day'+i][v.dinner_type_id][v.sales_type].spicy++;
            };
            Count['day'+i][v.dinner_type_id][v.sales_type].total++;

            if(v['day'+i+'_isFried'] ){
                Count['day'+i][v.dinner_type_id].fried++;
            }
            if(v['day'+i+'_isGreen'] ){
                Count['day'+i][v.dinner_type_id][v.sales_type].green++;
            }

            let key;
            if(v.isSpecialGroups){
                key = 'special_'+v.dish_num_id;
            }else{
                key = 'common';
            }
            !Count['day'+i][v.dinner_type_id][key] && (Count['day'+i][v.dinner_type_id][key]={});
            !Count['day'+i][v.dinner_type_id][key][v.dish_top_category_id] && (Count['day'+i][v.dinner_type_id][key][v.dish_top_category_id] = {
                dish_key_name:[]
            });
            if(v['day'+i+'_dish_key_name']){
                Count['day'+i][v.dinner_type_id][key][v.dish_top_category_id].dish_key_name.push(v['day'+i+'_dish_key_name'])
            }

            // 重复计数 中餐 晚餐  特色配菜不计算本周重复
            if(v['day'+i+'_key_id'] && (v.dinner_type == 'dn2' || v.dinner_type == 'dn3') && !v.isSpecialGroups){
                !Count.dish_key_id[v['day'+i+'_key_id']] && (Count.dish_key_id[v['day'+i+'_key_id']]=0);
                Count.dish_key_id[v['day'+i+'_key_id']]++;
            }
        }

        if(v.dish_num_id){
            !Count.num_and_top_category[v.dinner_type_id] && (Count.num_and_top_category[v.dinner_type_id] = []);
            Count.num_and_top_category[v.dinner_type_id].push(v.dish_num_id + '|' + v.dish_top_category_id);
        }
    });
    console.log('--------表格汇总----------');
    console.log(Count);
    return Count;
}

function toObj(arr,k) {
    let _Obj = {};
    for(let i = 0; i　< arr.length; i++) {
        _Obj[arr[i][k]] = arr[i];
    }
    return _Obj;
}

function evalFn(num,condition) {
    // console.log(num + condition);
    return eval(num + condition)
}

export {
    getRowItemData,getRowData,getCountRowData,clearLimitCategoryId,toObj
}
